package view;

import java.awt.*;

import java.util.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;

import model.Field;
import model.Values;

/**
 * A class which makes a Histogram where you can see back 
 * into the past of the field for a maximum of a hundred steps.
 * 
 * @author Martin Kromkamp and Albert-Jan Boer.
 * @version 2009.01.28
 */

public class HistogramView extends StatsView implements ActionListener {

	//Histogram
	private Histogram histogram;
	//Slider
	private JSlider stepsBackSlider;
	//Button
	private JButton goBackButton;
	//Panel
	private JPanel historyPanel;
	//Values
	private Values value;
	
	/**
     * Make a new HistogramView
     * @param the width of the view.
     * @param the height of the view.
     * @param the maximum amount of the histograms bar height.
     * @param field.
     * @param value.
     */
	public HistogramView(int width, int height, int maximumAmount, Field field, Values value) {
		super(width, height, maximumAmount, field);
		this.value = value;
		createFrame("Histogram");
		histogram = new Histogram(50);
		frame.getContentPane().add(histogram);
		
		historyPanel = new JPanel( new GridLayout( 2, 1 ) );
			//Slider
		stepsBackSlider = new JSlider( 0, 100, 50 );
		stepsBackSlider.setMajorTickSpacing(10);
		stepsBackSlider.setMinorTickSpacing(1);
		stepsBackSlider.setPaintTicks(true);
		stepsBackSlider.setPaintLabels(true);
		historyPanel.add( stepsBackSlider );
			//Button
		goBackButton = new JButton("Show history");
		goBackButton.addActionListener( this );
		historyPanel.add( goBackButton );
	
		frame.getContentPane().add( historyPanel, BorderLayout.SOUTH );

	}
	
	/**
     * Update the histogram with new field information.
     * @param the updated field.
     */
	public void updateHistogram(Field newField)
	{	
		field = newField;
		histogram.updateChart();		
	}
	
	
	private class Histogram extends JPanel {
		// The width of one rectangle on the histogram
		private int rectangleWidth;
				
		/**
		 * Creates the histogram
		 * @param rectWidth The width of one rectangle on the histogram
		 */
		public Histogram(int rectWidth) {
			rectangleWidth = rectWidth;
		}
		
		/**
		 * Paint the histogram.
		 * @param g.
		 */
		public void paint(Graphics g) {
		    super.paint(g);
		    
		    drawBackground(g);

		    Iterator<Class> it = stats.getClasses(field).iterator();
		    
		    //loop through the actorclasstypes and draw a histogramrectangle for each type
		    for(int i = 0; it.hasNext(); i+= rectangleWidth) {
		    	Class actorClass = it.next();
		    	
		    	// Draw a rectangle for the class
		    	int amount = stats.getAmount(actorClass, field);
		    	g.setColor(getColor(actorClass));
				g.fillRect(pixelsLeft + i, Math.round(statsViewHeight - amount / amountPerPixel), rectangleWidth, Math.round(amount / amountPerPixel));
				
				// Write information about the class (e.g. Rabbit 250)
				g.setColor(Color.BLACK);
				g.drawString(actorClass.toString().substring(12), pixelsLeft + 5 + i, statsViewHeight + 15);
				g.drawString("" + amount, pixelsLeft + 5 + i, statsViewHeight + 30);
		    }
		}
		
		/**
		 * Paints the background of the histogram chart
		 * @param g.
		 */
		private void drawBackground(Graphics g)
		{
			setBackground(Color.WHITE);
			
			g.setColor(Color.BLACK);
			g.drawLine(pixelsLeft - 1, 0, pixelsLeft - 1, statsViewHeight);
			g.drawLine(pixelsLeft, statsViewHeight, height, statsViewHeight);
			
			// write 'y-values' on the left side of the histogram.
			for(int i = 0; i <= maximumAmount; i += maximumAmount / 10) {
				g.drawString("" + i, 2, statsViewHeight + 2 - i / amountPerPixel);
			}
			
			// draw horizontal lines in the histogram
			g.setColor(Color.gray);
			for(int i = statsViewHeight - 50; i > 0; i-= 50) {
				g.drawLine(pixelsLeft, i, height, i);
			}
		}
		
		/**
		 * Repaints the histogram
		 */
		public void updateChart()
		{
			repaint();
		}
		
		
	}
	
	/**
     * The action checker which waits for a button to be pushed.
     * @param the button beeing pushed.
     */
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == goBackButton){
			Field updateField = value.fieldHistory.get( value.fieldHistory.size() - stepsBackSlider.getValue() );
			this.updateHistogram( updateField );
		}
	}	
}
